package cn.dnui_zzh807.dnuifood_16110100807.model;


import java.util.List;

import cn.dnui_zzh807.dnuifood_16110100807.bean.CommentInfo;
import cn.dnui_zzh807.dnuifood_16110100807.bean.LoginBean;
import cn.dnui_zzh807.dnuifood_16110100807.bean.ResultBean;
import cn.dnui_zzh807.dnuifood_16110100807.bean.UserInfo;
import cn.dnui_zzh807.dnuifood_16110100807.listener.OnRetrofitListener;
import cn.dnui_zzh807.dnuifood_16110100807.service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Dobby on 2018/10/15.
 */

public class UserModel extends RetrofitBaseModel {
    private final UserService uerService;
    private UserService service;
    private Retrofit retrofit;
    private static final String BASE_URL = "http://172.24.10.175:8080/foodService/";

    public UserModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        this.uerService = retrofit.create(UserService.class);
    }

    public void userLogin(String username, String password, final OnRetrofitListener listener) {
//        Call call = uerService.userLogin(username, password);
//        bindCallback(call, listener, Constants.USERLOGIN);

        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserService userService = retrofit.create(UserService.class);

        Call<LoginBean> call = userService.userLogin(username,password);
        Callback<LoginBean> callback = new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body(),1);


            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail();
            }

        };
        call.enqueue(callback);
    }

    public void register(String username, String userpass, String mobilenum, String address, String comment, final OnRetrofitListener listener) {
//        Call call = uerService.register(username,userpass,mobilenum,address,comment);
//        bindCallback(call,listener,Constants.USERREGISTER);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserService userService = retrofit.create(UserService.class);
        Call<ResultBean> call =userService.register(username,userpass,mobilenum,address,comment);
        Callback<ResultBean> callback = new Callback<ResultBean>() {
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                listener.onSuccess(response.body(),2);
            }

            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    public void getUserById(String user_id,final OnRetrofitListener listener){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserService userService = retrofit.create(UserService.class);
        Call<UserInfo> call = userService.getUserById(user_id);
        Callback<UserInfo> callback = new Callback<UserInfo>() {
            @Override
            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
                listener.onSuccess(response.body(),9);
            }

            @Override
            public void onFailure(Call<UserInfo> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    public void updateUserById(String user_id,String username,String userpass,String mobilenum,String address,final OnRetrofitListener listener){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserService userService = retrofit.create(UserService.class);
        Call<ResultBean> call = userService.updateUserById(user_id,username,userpass,mobilenum,address);
        Callback<ResultBean> callback = new Callback<ResultBean>() {
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                listener.onSuccess(response.body(),10);
            }

            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }


    public void getAllOrdersByUser(String user_id,final OnRetrofitListener listener){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserService userService = retrofit.create(UserService.class);
        Call<List<CommentInfo>> call = userService.getAllOrdersByUser(user_id);
        Callback<List<CommentInfo>> callback = new Callback<List<CommentInfo>>() {
            @Override
            public void onResponse(Call<List<CommentInfo>> call, Response<List<CommentInfo>> response) {
                listener.onSuccess(response.body(),14);
            }

            @Override
            public void onFailure(Call<List<CommentInfo>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }


    public void getAllCommentsByUser(String user_id,final OnRetrofitListener listener){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserService userService = retrofit.create(UserService.class);
        Call<List<CommentInfo>> call = userService.getAllCommentsByUser(user_id);
        Callback<List<CommentInfo>> callback = new Callback<List<CommentInfo>>() {
            @Override
            public void onResponse(Call<List<CommentInfo>> call, Response<List<CommentInfo>> response) {
                listener.onSuccess(response.body(),15);
            }

            @Override
            public void onFailure(Call<List<CommentInfo>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
}
